package com.tony.mindweather.MyTimer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tony.mindweather.config.SayLoveConfig;
import com.tony.mindweather.entity.Forecast;
import com.tony.mindweather.entity.Weather;
import com.tony.mindweather.entity.WeatherResponse;
import com.zjiecode.wxpusher.client.WxPusher;
import com.zjiecode.wxpusher.client.bean.Message;
import com.zjiecode.wxpusher.client.bean.MessageResult;
import com.zjiecode.wxpusher.client.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @Classname MyTimer
 * @Description TODO
 * @Date 2022/2/7 11:37
 * @Created by
 */
@Component
public class MyTimerToWeChat {
    /**
     * 邮箱号
     */
    @Value("${spring.mail.username}")
    private String email;

    @Value("${sayloveurlAPIKEY}")
    private String sayloveurl;

    @Value("${city}")
    private String city;

    @Value("${weChatMessageToken}")
    private String weChatMessageToken;

    @Value("${TonyWeChatID}")
    private String TonyWeChatID;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private SayLoveConfig sayLoveConfig;

    private int loveday = 2;//相爱天数



    /**
     * 定时发送微信
     */
    @Async // 开启异步
    @Scheduled(cron = "0 50 7/23 * * ? ") // 设置定时时间 ，每天7点50发送消息
//    @Scheduled(cron = "0/10 0/1 * * * ? ") // 设置定时时间 每10秒
    public void process() {

        /*天气API*/
        String apiURL = "http://wthrcdn.etouch.cn/weather_mini?city=上海";
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(apiURL, String.class);// 发起天气请求
        ObjectMapper objectMapper = new ObjectMapper();
        WeatherResponse resp = null;
        String strBody = null;
        if (responseEntity.getStatusCodeValue() == 200) {
            strBody = responseEntity.getBody();
        }
        try {
            resp = objectMapper.readValue(strBody, WeatherResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        final Weather data = resp.getData();
        final String weathercity = data.getCity();
        final String ganmao = data.getGanmao();
        final String wendu = data.getWendu();
        final Forecast forecast = data.getForecast().get(0);
        final String high = forecast.getHigh();
        final String low = forecast.getLow();
        final String type = forecast.getType();
        String showWeather =  weathercity+type + ",最" + high + ",最" + low + ",当前温度" + wendu + "。";

        // 处理土味情话
        String sayLoveAPIURL = "http://api.tianapi.com/saylove/index?key=" + sayloveurl;
        final String sayLoveString = sayLoveConfig.SayLoveRequest(sayLoveAPIURL);
        final String[] split = sayLoveString.split("\":\"");
        final String SayLoveContent = split[split.length - 1].split("\"")[0];

        /*要发送的内容*/
        String showWeatherWithSayLove =
                "早安呀,小宝贝，今天是小猪爱你的第" + loveday + "天。\r\n下面由小猪为小宝贝播报" + weathercity + "的天气：" + showWeather + "\r\n" +
                        "今日情话：" + SayLoveContent + "——爱你的小猪！";

        /*发送微信*/
        Message weChatMessage = new Message();
        weChatMessage.setAppToken(weChatMessageToken);
        weChatMessage.setContentType(Message.CONTENT_TYPE_TEXT);
        weChatMessage.setContent(showWeatherWithSayLove);
        weChatMessage.setUid(TonyWeChatID); // 发给个人
//        weChatMessage.setUid(SheepWeChatID);
//        final Set<String> uids = new HashSet<>();
//        uids.add(hadwinlingWeChatID);
//        uids.add(SheepWeChatID);
//        weChatMessage.setUids(uids); // 发给uid 集合
        Result<List<MessageResult>> result = WxPusher.send(weChatMessage);

        loveday = loveday + 1;

    }
}
